package com.czl.module_storehouse.activity.receive.details;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.LayoutInflater;

import com.czl.base.data.bean.tengyun.ReceiveBean;
import com.czl.base.data.bean.tengyun.SortBean;
import com.czl.module_base.activity.BaseActivity;
import com.czl.module_storehouse.R;
import com.czl.module_storehouse.adapter.ReceiveDetailsAdapter;
import com.czl.module_storehouse.databinding.ActivityReceiveDetailsBinding;
import com.czl.module_storehouse.databinding.FooterRecordInfoBinding;
import com.czl.module_storehouse.databinding.HeaderReceiveDetailsBinding;
import com.czl.module_storehouse.event.ReceiveEvent;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.List;

/**
 * @author xubin
 */
public class ReceiveDetailsActivity extends BaseActivity<ActivityReceiveDetailsBinding> {

    private ReceiveDetailsAdapter mAdapter;

    @Override
    protected ActivityReceiveDetailsBinding getBinding(LayoutInflater layoutInflater) {
        return ActivityReceiveDetailsBinding.inflate(layoutInflater);
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        mToolBinding.toolbarContentTitle.setText("领用详情");
        mAdapter = new ReceiveDetailsAdapter(R.layout.item_receive_details, "已领：");
        binding.recyclerView.setAdapter(mAdapter);
        EventBus.getDefault().register(this);
    }

    @SuppressLint("SetTextI18n")
    private void initHeaderView(HeaderReceiveDetailsBinding detailsBinding, ReceiveBean bean) {
        detailsBinding.clHeaderInfo.ivHeader.setImageResource(R.mipmap.icon_lingyong_head);
        if (bean == null) {
            return;
        }
        detailsBinding.clHeaderInfo.tvHeaderTitle.setText(bean.getReceiveName());
        detailsBinding.clHeaderInfo.tvHeaderDate.setText(bean.getReceiveDate());
        detailsBinding.clHeaderInfo.tvHeaderCode.setText("领用单：" + (bean.getReceiveCode() == null ? "" : bean.getReceiveCode()));
        detailsBinding.tvHeaderDepartment.setText("领用部门：" + (bean.getReceivePersonDepartment() == null ? "" : bean.getReceivePersonDepartment()));
        detailsBinding.tvHeaderDesc.setText("领用原因：" + (bean.getApplyComment() == null ? "" : bean.getApplyComment()));
    }

    @Subscribe(threadMode = ThreadMode.POSTING, sticky = true)
    public void onDataEvent(ReceiveEvent receiveEvent) {
        ReceiveBean receiveBean = receiveEvent.getReceiveBean();
        List<SortBean> sortList = receiveBean.getSortList();
        if (sortList != null) {
            mAdapter.setList(sortList);
        }
        HeaderReceiveDetailsBinding detailsBinding = HeaderReceiveDetailsBinding.inflate(getLayoutInflater());
        initHeaderView(detailsBinding, receiveBean);
        mAdapter.addHeaderView(detailsBinding.getRoot());
        FooterRecordInfoBinding infoBinding = FooterRecordInfoBinding.inflate(getLayoutInflater());
        infoBinding.tvRemark.setText(receiveBean.getReceiveComment());
        mAdapter.addFooterView(infoBinding.getRoot());
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}
